package com.data.modules.jhl.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.alibaba.fastjson.JSONObject;
import com.data.common.util.DateUtils;
import com.data.modules.jhl.entity.SmJhlSleep;
import com.data.modules.jhl.entity.SmJhlSleep;
import com.data.modules.jhl.mapper.SmJhlSleepMapper;
import com.data.modules.jhl.mapper.SmJhlSleepMapper;
import com.data.modules.jhl.service.ISmJhlSleepService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 精华隆睡眠日报
 * @Author: data-boot
 * @Date:   2023-05-25
 * @Version: V1.0
 */
@Service
public class SmJhlSleepServiceImpl extends ServiceImpl<SmJhlSleepMapper, SmJhlSleep> implements ISmJhlSleepService {

    @Override
    public void saveSleepInfo(JSONObject jsonObject, String mac) {

        String sleepStart = jsonObject.getString("sleep_start");
        String sleepEnd = jsonObject.getString("sleep_end");
        String goBed = jsonObject.getString("go_bed");
        String leaveBed = jsonObject.getString("leave_bed");
        String evaluation = jsonObject.getString("evaluation");
        String baseRespiratory = jsonObject.getString("base_respiratory");
        String heartRate = jsonObject.getString("base_heart_rate");
        String baseBodyMovement = jsonObject.getString("base_body_movement");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换

        SmJhlSleep smJhlSleep =new SmJhlSleep();
        smJhlSleep.setMac(mac)
                .setSleepStart(sdf.format(new Date(Long.parseLong(sleepStart)*1000)))
                .setSleepEnd(sdf.format(new Date(Long.parseLong(sleepEnd)*1000)))
                .setGoBed(sdf.format(new Date(Long.parseLong(goBed)*1000)))
                .setLeaveBed(sdf.format(new Date(Long.parseLong(leaveBed)*1000)))

                .setEvaluation(evaluation)
                .setBaseRespiratory(baseRespiratory)
                .setBodyMovement(baseBodyMovement)
                .setHeartRate(heartRate)
                .setEventTime(DateUtil.beginOfDay(DateUtil.yesterday()));

        this.save(smJhlSleep);
    }
}
